import router from '@/router'
import {post} from '@/util/request'

export default {
	namespace:'job',
	namespaced:true,
	state:{
		isLoading:false,
		isModalLoading:false,
		pageData: {},
		detail: {},
		authList: []
	},
	mutations:{
		changeLoading(state,v){
			state.isLoading = v;
		},
		changeModalLoading(state,v){
			state.isModalLoading = v;
		},
		changePagedata(state,v){
			state.pageData = v;
		},
		changeDetail(state,v){
			state.detail = v;
		},
		changeAuthlist(state,v) {
			state.authList = v;
		}
	},
	actions:{
		async reload({commit},data){
			commit('changeLoading',true);
			let res = await post('/auth/job/list', data);
			commit('changePagedata',res.data.data);
			commit('changeLoading',false);
		},
		async loadView({commit},id){
			commit('changeModalLoading',true);
			let res = await post('/auth/job/view', {jobId:id});
			commit('changeDetail',res.data.data);
			commit('changeModalLoading',false);
		},
		async loadAuthList({commit},data){
			commit('changeModalLoading',true);
			let res = await post('/auth/job/getZtree',data);
			let lst = res.data.data.tree.map(v => {
				return {id: v.id, parentid: v.parentid, title: v.name, expand: v.open, disableCheckbox: v.chkDisabled, checked: v.checked};
			});
			commit('changeAuthlist',lst);
			commit('changeModalLoading',false);
		},
		submitAdd({commit},form){
			commit('changeModalLoading',true);
			return post('/auth/job/addsave', form).then(json => {
				commit('changeModalLoading',false);
				return json.data;
			});
		},
		submitEdit({commit},form){
			commit('changeModalLoading',true);
			return post("/auth/job/updatesave",form).then(json => {
				commit('changeModalLoading',false);
				return json.data;
			});
		},
		deleteJob({commit},jobId){
			commit('changeModalLoading',true);
			return post('/auth/job/delete',{jobId}).then(json => {
				commit('changeModalLoading',false);
				return json.data;
			});
		}
	},
	subscriptions({dispatch},router){
		//给路由加监听一旦进入module绑定的页面时dispatch初始化action
		router.afterEach((to,from)=>{
			if(to.name === this.namespace){
				dispatch('reload',{})
			}
		})
	}
}
